﻿Imports System
Imports System.CodeDom
Imports System.Globalization
Imports System.Text
Imports System.Workflow.ComponentModel.Compiler

Namespace System.Workflow.Activities.Rules
    Friend Class DirectionExpression
        Inherits RuleExpressionInternal
        ' Methods
        Friend Overrides Sub AnalyzeUsage(ByVal expression As CodeExpression, ByVal analysis As RuleAnalysis, ByVal isRead As Boolean, ByVal isWritten As Boolean, ByVal qualifier As RulePathQualifier)
            Dim expression2 As CodeDirectionExpression = DirectCast(expression, CodeDirectionExpression)
            Dim expression3 As CodeExpression = expression2.Expression
            Dim flag As Boolean = False
            Dim flag2 As Boolean = True
            Dim qualifier2 As RulePathQualifier = Nothing
            Select Case expression2.Direction
                Case FieldDirection.In
                    flag = False
                    flag2 = True
                    qualifier2 = New RulePathQualifier("*", Nothing)
                    Exit Select
                Case FieldDirection.Out
                    flag = True
                    flag2 = False
                    qualifier2 = Nothing
                    Exit Select
                Case FieldDirection.Ref
                    flag = True
                    flag2 = True
                    qualifier2 = If(analysis.ForWrites, Nothing, New RulePathQualifier("*", Nothing))
                    Exit Select
            End Select
            RuleExpressionWalker.AnalyzeUsage(analysis, expression3, flag2, flag, qualifier2)
        End Sub

        Friend Overrides Function Clone(ByVal expression As CodeExpression) As CodeExpression
            Dim expression2 As CodeDirectionExpression = DirectCast(expression, CodeDirectionExpression)
            Return New CodeDirectionExpression { _
                .Direction = expression2.Direction, _
                .Expression = RuleExpressionWalker.Clone(expression2.Expression) _
            }
        End Function

        Friend Overrides Sub Decompile(ByVal expression As CodeExpression, ByVal stringBuilder As StringBuilder, ByVal parentExpression As CodeExpression)
            Dim expression2 As CodeDirectionExpression = DirectCast(expression, CodeDirectionExpression)
            Dim str As String = Nothing
            If (expression2.Direction = FieldDirection.Out) Then
                str = "out "
            ElseIf (expression2.Direction = FieldDirection.Ref) Then
                str = "ref "
            End If
            If (Not str Is Nothing) Then
                stringBuilder.Append(str)
            End If
            RuleExpressionWalker.Decompile(stringBuilder, expression2.Expression, expression2)
        End Sub

        Friend Overrides Function Evaluate(ByVal expression As CodeExpression, ByVal execution As RuleExecution) As RuleExpressionResult
            Dim expression2 As CodeDirectionExpression = DirectCast(expression, CodeDirectionExpression)
            Return RuleExpressionWalker.Evaluate(execution, expression2.Expression)
        End Function

        Friend Overrides Function Match(ByVal expression As CodeExpression, ByVal comperand As CodeExpression) As Boolean
            Dim expression2 As CodeDirectionExpression = DirectCast(expression, CodeDirectionExpression)
            Dim expression3 As CodeDirectionExpression = DirectCast(comperand, CodeDirectionExpression)
            Return ((expression2.Direction = expression3.Direction) AndAlso RuleExpressionWalker.Match(expression2.Expression, expression3.Expression))
        End Function

        Friend Overrides Function Validate(ByVal expression As CodeExpression, ByVal validation As RuleValidation, ByVal isWritten As Boolean) As RuleExpressionInfo
            Dim info As RuleExpressionInfo
            Dim flag As Boolean
            Dim expression2 As CodeDirectionExpression = DirectCast(expression, CodeDirectionExpression)
            If isWritten Then
                Dim item As New ValidationError(String.Format(CultureInfo.CurrentCulture, Messages.CannotWriteToExpression, New Object() { GetType(CodeDirectionExpression).ToString }), &H17A)
                item.UserData.Item("ErrorObject") = expression2
                validation.Errors.Add(item)
                Return Nothing
            End If
            If (expression2.Expression Is Nothing) Then
                Dim error2 As New ValidationError(Messages.NullDirectionTarget, &H53D)
                error2.UserData.Item("ErrorObject") = expression2
                validation.Errors.Add(error2)
                Return Nothing
            End If
            If TypeOf expression2.Expression Is CodeTypeReferenceExpression Then
                Dim error As New ValidationError(String.Format(CultureInfo.CurrentCulture, Messages.CodeExpressionNotHandled, New Object() { expression2.Expression.GetType.FullName }), &H548)
                [error].UserData.Item("ErrorObject") = expression2.Expression
                validation.AddError([error])
                Return Nothing
            End If
            If (expression2.Direction = FieldDirection.Ref) Then
                flag = True
                If (RuleExpressionWalker.Validate(validation, expression2.Expression, False) Is Nothing) Then
                    Return Nothing
                End If
                info = RuleExpressionWalker.Validate(validation, expression2.Expression, True)
            ElseIf (expression2.Direction = FieldDirection.Out) Then
                flag = True
                info = RuleExpressionWalker.Validate(validation, expression2.Expression, True)
            Else
                flag = False
                info = RuleExpressionWalker.Validate(validation, expression2.Expression, False)
            End If
            If (info Is Nothing) Then
                Return Nothing
            End If
            Dim expressionType As Type = info.ExpressionType
            If (expressionType Is Nothing) Then
                Return Nothing
            End If
            If (((Not expressionType Is GetType(NullLiteral)) AndAlso flag) AndAlso Not expressionType.IsByRef) Then
                expressionType = expressionType.MakeByRefType
            End If
            Return New RuleExpressionInfo(expressionType)
        End Function

    End Class
End Namespace

